/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cageballserver.match;

import cageballcommon.messages.Action;
import cageballcommon.messages.PlayerAction;
import cageballcommon.enummerations.TeamEnum;
import cageballcommon.messages.InitPlayer;
import cageballcommon.messages.InitTeam;
import cageballcommon.messages.Ball;
import cageballcommon.messages.Info;
import cageballcommon.messages.Player;
import cageballcommon.messages.State;
import cageballcommon.messages.Team;
import java.util.ArrayList;
import java.util.Properties;

/**
 *
 * @author kanonas
 */
public class Inverter {

    private Properties game;
    private float fieldLengthX;
    private float fieldLengthY;

    public Inverter(Properties game) {
        this.game = game;
        fieldLengthX = Float.parseFloat(game.getProperty("fieldLengthX"));
        fieldLengthY = Float.parseFloat(game.getProperty("fieldLengthY"));
    }

    public InitTeam invertInitTeam(InitTeam initTeam) {
        ArrayList<InitPlayer> list = new ArrayList<InitPlayer>();

        for (InitPlayer p : initTeam.getPlayers()) {
            list.add(invertInitPlayer(p));
        }
        InitTeam tmp = new InitTeam(initTeam);
        tmp.setPlayers(list);
        return tmp;
    }

    public InitPlayer invertInitPlayer(InitPlayer initPlayer) {
        InitPlayer tmp = new InitPlayer(initPlayer);
        tmp.setXStart(fieldLengthX - tmp.getXStart());
        tmp.setYStart(fieldLengthY - tmp.getYStart());
        return tmp;
    }

    public State invertState(State state) {
        return new State(state.getTime(), invertBall(state.getBall()), invertTeam(state.getOtherTeam()), invertTeam(state.getMyTeam()), invertInfo(state.getInfo()));

    }

    public Team invertTeam(Team team) {
        ArrayList<Player> list = new ArrayList<Player>();

        for (Player p : team.getPlayers()) {
            list.add(invertPlayer(p));
        }
        Team tmp = new Team(team);
        tmp.setPlayers(list);
        return tmp;
    }

    public Player invertPlayer(Player player) {
        Player tmp = new Player(player);
        tmp.setX(fieldLengthX - tmp.getX());
        tmp.setY(fieldLengthY - tmp.getY());
        return tmp;
    }

    public Ball invertBall(Ball ball) {
        Ball tmp = new Ball(ball);
        tmp.setX(fieldLengthX - tmp.getX());
        tmp.setY(fieldLengthY - tmp.getY());
        return tmp;
    }

    public Info invertInfo(Info info) {
        if (info != null) {
            Info tmp = new Info(info);
            tmp.setGoal(invertTeamEnum(info.getGoal()));
            tmp.setScorerTeam(invertTeamEnum(info.getScorerTeam()));
            tmp.setWinner(invertTeamEnum(info.getWinner()));

            return tmp;
        } else {
            return null;
        }

    }

    public Action invertAction(Action action) {
        ArrayList<PlayerAction> list = new ArrayList<PlayerAction>();

        for (PlayerAction p : action.getActions()) {
            if (p != null) {
                list.add(invertPlayerAction(p));
            }
        }

        Action tmp = new Action(action);
        tmp.setActions(list);
        return tmp;
    }

    public PlayerAction invertPlayerAction(PlayerAction playerAction) {
        PlayerAction tmp = new PlayerAction(playerAction);
        tmp.setMoveX(fieldLengthX - tmp.getMoveX());
        tmp.setMoveY(fieldLengthY - tmp.getMoveY());
        tmp.setKickX(fieldLengthX - tmp.getKickX());
        tmp.setKickY(fieldLengthY - tmp.getKickY());
        return tmp;
    }

    public TeamEnum invertTeamEnum(TeamEnum teamEnum) {
        if (teamEnum == TeamEnum.myTeam) {
            return TeamEnum.otherTeam;
        } else if (teamEnum == TeamEnum.otherTeam) {
            return TeamEnum.myTeam;
        } else {
            return teamEnum;
        }
    }
}
